package com.tjsinfo.tjsoa.doc.action;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.doc.bo.ITDocDocBO;
import com.tjsinfo.tjsoa.doc.vo.TDocDoc;
import com.tjsinfo.tjsoa.workflow.WorkFlowTemplate;

public class DocAddAction extends Action {
	private ITDocDocBO docBo;
	public void setDocBo(ITDocDocBO docBo) {
		this.docBo = docBo;
	}


	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date = new Date();
			Integer docMark = Integer.parseInt(request.getParameter("docMark"));
			String docName = request.getParameter("docName");
			String docUse = request.getParameter("docUse");
			String status = "审核中";
			String remarks = request.getParameter("remarks");
			Integer applyId = ToolKit.getSessionUser(request).getUser_id();
			String applyName = ToolKit.getSessionUser(request).getUsername();
			String deptId = request.getParameter("deptId");
			String officeId = request.getParameter("officeId");
			
			String linkurl = null;
			String message = null;
			
			WorkFlowTemplate test = new WorkFlowTemplate();
			Long flowid = null;
			switch (docMark) {
			case 0:
				flowid = test.creatProcessInstance("办公室登记",officeId,"docOri",null);
				test.setVariable("归还",officeId, flowid);
				
				linkurl = "doc/ori/oriList.jsp";
				message = "doc.oriAdd.message";
				break;
			case 1:
				flowid = test.creatProcessInstance("部长审批",deptId,"docCerCopy",null);
				test.setVariable("办公室主管审批",officeId, flowid);
				test.setVariable("归还",officeId, flowid);
				
				linkurl = "doc/cer/cerList.jsp";
				message = "doc.cerAdd.message";
				break;
			case 2:
				flowid = test.creatProcessInstance("部长审批",deptId,"docCerCopy",null);
				test.setVariable("办公室主管审批",officeId, flowid);
				
				linkurl = "doc/copy/copyList.jsp";
				message = "doc.copyAdd.message";
				break;				
			}
			
			TDocDoc tdd = new TDocDoc();
			tdd.setFlowid(Integer.parseInt(flowid.toString()));
			tdd.setDateTime(sdf.parse(sdf.format(date)));
			//tdd.setDeptId(deptId);
			//tdd.setDeptName(deptName);			
			tdd.setApplyId(applyId.toString());
			tdd.setApplyName(applyName);
			tdd.setDocName(docName);
			tdd.setDocUse(docUse);
			tdd.setStatus(status);
			tdd.setRemarks(remarks);
			tdd.setUserId(",");
			tdd.setMark(docMark);
			docBo.save(tdd);
			
			request.setAttribute("linkurl", linkurl);
			request.setAttribute("message", message);
			
			return mapping.findForward("success");
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("failed");
		}
	}
}
